<%@page session="false"%>
<%@page import="java.net.*,java.io.*" %>
<%@ page import="java.util.regex.Pattern" %>
<%@ page import="java.util.regex.Matcher" %>
<%@ page pageEncoding="UTF-8" isELIgnored="false" %>
<%
	/**
	 *为实现客户端跨域请求而采用jsp制作的代理转发器
	 *使用方式：
	 *将该文件放在iserver webapps下面你的工程目录下，请求格式：http://localhost:8090/yourRoot/Proxy.jsp?url=yoururl,注意url参数中特殊字符需要转码。
	 *支持post，get请求
	 */
	try {
		String reqUrl = request.getParameter("url");
		response.setContentType("image/png");
		response.setCharacterEncoding("utf-8");
		//将参数中临时替换的字符替换回来
		reqUrl = reqUrl.replaceAll("\\$","&");
		reqUrl = reqUrl.replace("!", "?");

		//将参数中的中文进行编码
		Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]");
		Matcher m = pattern.matcher(reqUrl);
		String zhStr = "";
		while (m.find()) {
			String t = m.group();
			zhStr += t;
		}
		if(!"".equals(zhStr)){
			try {
				reqUrl = reqUrl.replaceAll(zhStr,URLEncoder.encode(zhStr,"utf-8"));
			} catch (UnsupportedEncodingException e) {
				System.out.println("编码转换出错！ 地图参数中含有中文字符");
				e.printStackTrace();
			}
		}
		System.out.println(reqUrl);
		URL url = new URL(reqUrl);
		HttpURLConnection connection = (HttpURLConnection)url.openConnection();
		DataInputStream in = new DataInputStream(connection.getInputStream());
		OutputStream outputStream = response.getOutputStream();
		byte[] buffer = new byte[512];//缓冲字节数，值小适用网络宽，降低丢包率
		int count = 0;
		while ((count = in.read(buffer)) > 0){
			outputStream.write(buffer, 0, count);
		}
		outputStream.close();
		in.close();
		connection.disconnect();

	} catch(Exception e) {
		System.out.println(0);
		System.out.println(e);
		response.setStatus(500);
	}
%>